package com.lbd99.data.utils;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author caiwenhong
 * 字符 处理工具类
 */
public class StrUtil {

	private static final char[] CH_TBL = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
	private final String KEY = "power&201801";
	public static final String INIT_PWD = "123123";
	private static byte[] desKey = "power&201801".getBytes();

	protected static byte[] execute(int mode, byte[] buffer) throws Exception {
		SecureRandom sr = new SecureRandom();
		DESKeySpec dks = new DESKeySpec(desKey);
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
		SecretKey key = keyFactory.generateSecret(dks);
		Cipher cipher = Cipher.getInstance("DES");
		cipher.init(mode, key, sr);
		return cipher.doFinal(buffer);
	}

	/**
	 *  加密
	 * @param src
	 * @return
	 */
	public static   String doEncrypt(String src) {
		try {
			byte[] ex = execute(1, src.getBytes());
			if(ex != null && ex.length > 0) {
				String result = "";

				for(int i = 0; i < ex.length; ++i) {
					int b = 255 & ex[i];
					result = result + CH_TBL[b >> 4] + CH_TBL[15 & b];
				}
				return result;
			} else {
				return "";
			}
		} catch (Exception var6) {
			throw new RuntimeException(var6);
		}
	}

	private static int CharToInt(char ch) {
		return ch >= 48 && ch <= 57?ch - 48:(ch >= 65 && ch <= 70?ch - 65 + 10:(ch >= 97 && ch <= 102?ch - 97 + 10:-1));
	}

	/**
	 *  解密
	 * @param src
	 * @return
	 */
	public static String doDecrypt(String src) {
		try {
			if(src != null && src.length() % 2 == 0) {
				byte[] ex = new byte[src.length() / 2];
				String tmp = src.toUpperCase();
				int i = 0;

				for(int index = 0; i < tmp.length(); ++index) {
					int v1 = CharToInt(tmp.charAt(i));
					int v2 = CharToInt(tmp.charAt(i + 1));
					if(v1 == -1 || v2 == -1) {
						return "";
					}

					ex[index] = (byte)(v1 << 4 | v2);
					i += 2;
				}

				return new String(execute(2, ex));
			} else {
				return "";
			}
		} catch (Exception var8) {
			throw new RuntimeException(var8);
		}
	}

	
	/**
	 * 判断某个对象是否为空 集合类、数组做特殊处理
	 * 
	 * @param obj
	 * @return 如为空，返回true,否则false
	 * @author caiwenhong 
	 */
	public static boolean isEmpty(Object obj) {
		if (obj == null)
			return true;

		// 如果不为null，需要处理几种特殊对象类型
		if (obj instanceof String) {
			return obj.equals("");
		} else if (obj instanceof Collection) {
			// 对象为集合
			Collection coll = (Collection) obj;
			return coll.size() == 0;
		} else if (obj instanceof Map) {
			// 对象为Map
			Map map = (Map) obj;
			return map.size() == 0;
		} else if (obj.getClass().isArray()) {
			// 对象为数组
			return Array.getLength(obj) == 0;
		} else {
			// 其他类型，只要不为null，即不为empty
			return false;
		}
	}


	/**
	 * 保留2位小数
	 */
	public static double keep4decimal(double f) {
		BigDecimal bg = new BigDecimal(f);
		double f1 = bg.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
		return f1;
	}
	
	//去掉list中重复的值
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static List removeDuplicateWithOrder(List list) {
        Set set = new HashSet();
        List newList = new ArrayList();
        for (Iterator iter = list.iterator(); iter.hasNext();) {
            Object element = iter.next();
            if (set.add(element))
                newList.add(element);
        }
        return newList;
    }

	public static void main(String[] args) {
		System.out.println(doEncrypt("111111"));
		//E4C4A80E60F23EE0B569E0E87E963A90
	}


	
}
